package me.zhyx.music.thread.producer;

import lombok.extern.slf4j.Slf4j;
import me.zhyx.music.analysis.HtmlAnalysis;
import me.zhyx.music.init.TaskQueue;
import me.zhyx.music.model.Proxy_ip;
import me.zhyx.music.utils.HttpClientUtils;
import org.apache.http.HttpHost;
import org.apache.http.conn.HttpHostConnectException;
import org.springframework.stereotype.Service;

import java.net.ConnectException;


/**
 * @Auther: yssq
 * @Date: 2019/3/14 12:56
 * @Description:
 */
@Slf4j
@Service
public class GatherThread implements Runnable {

    @Override
    public void run() {
        String url = null;
        for (; ; ) {
            try {
                url = TaskQueue.url.poll().getLink();
                if (url != null) {
                    HtmlAnalysis.getLink(HttpClientUtils.sendGet(url + "?time=" + System.currentTimeMillis(), null), url);
                }

//                Thread.sleep((int) (Math.random() * 8000 + 1000));
//              log.info("Get request success ,the url is {}", url);
            } catch (Exception e) {
                log.error("Exception! the url is {}", url);
//                try {
//                    Proxy_ip proxyIp = TaskQueue.proxy_ips.poll();
//                    HttpHost httpHost = new HttpHost(proxyIp.getIp(),proxyIp.getPort(),proxyIp.getType());
//                    HttpClientUtils.requestConfig(httpHost);
//                }catch (Exception e1){
//                    e1.printStackTrace();
//                }

            }
        }


    }


}
